package com.jackrain.nea.core.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.jackrain.nea.core.api.AdTableCategoryDelCmd;
import com.jackrain.nea.core.mapper.AdTableCategoryMapper;
import com.jackrain.nea.core.utility.CommandData;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class AdTableCategoryDelCmdImpl extends CommandAdapter implements AdTableCategoryDelCmd {

    @Autowired
    private AdTableCategoryMapper adTableCategoryMapper;

    @Override
    public ValueHolder execute(QuerySession session) throws NDSException {
        CommandData data = new CommandData(session);
        if (!data.isDeleteMainTable()) {
            throw data.notSupported();
        }
        long id = data.getObjId();
        if (adTableCategoryMapper.delete(id) == 1) { //尝试删除主数据
            return data.success();
        }
        if (!adTableCategoryMapper.exists(id)) { // 如果删除失败,判断id是否存在,如果存在说明还有关联数据没有删除
            return data.success();
        }

        log.info("return data-> error");
        return data.error(-1, "类别中存在表时无法删除，请先删除类别中的表！");
    }
}
